<template>
  <div class="table-box">
    <!-- <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" plain @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div> -->
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="渠道名称">
          <el-input v-model="form.name" placeholder="渠道名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="序号" align="center" />
          <el-table-column prop="pay_type" label="支付类型" align="center" />
          <el-table-column prop="name" label="名称" align="center" />
          <el-table-column prop="channel" label="APP渠道" align="center" />
          <el-table-column label="图片" align="center" width="100">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon_show"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.icon_show]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="country" label="国家名称" align="center" />
          <el-table-column
            prop="country_code"
            label="国家code码"
            align="center"
          />
          <el-table-column
            prop="country_currency"
            label="国家币种简称"
            align="center"
          />
          <el-table-column prop="rank" label="排序号" align="center" />
          <el-table-column
            prop="is_hide"
            label="是否隐藏"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            label="操作"
            align="center"
            width="320"
            fixed="right"
          >
            <template #default="scope">
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'edit')"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  @click="handleOperate(scope.$index, scope.row, 'delete')"
                  >删除</el-button
                >
                <el-button
                  type="success"
                  @click="handleIsHide(scope.$index, scope.row, 0)"
                  >生效</el-button
                >
                <el-button
                  class="mt-10"
                  type="info"
                  @click="handleIsHide(scope.$index, scope.row, 1)"
                  >不生效</el-button
                >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatter } from "@/utils/utils";
import {
  payChannelListApi,
  payChannelHideApi,
  payChannelDelApi,
  payChannelAddApi,
  payChannelUpdateApi,
} from "@/api/moby/payChannel/index";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const btnType = ref(null);
const formData = {
  name: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  name: "",
  country: "",
  country_code: "",
  country_currency: "",
  rank: "",
  pay_type:"",
  icon:"",
  iconUrl:"",
};
const dialogForm = reactive({ ...dialogFormData });
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
  };
};

const handleOperate = (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogVisible.value = true;
   
    Object.assign(dialogForm, { ...row ,iconUrl:row.icon_show});
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此渠道?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await payChannelDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};

//生不生效
const handleIsHide = (index, row, type) => {
  ElMessageBox.confirm(
    `您是否确认${type ? "不生效" : "生效"}此渠道?`,
    "温馨提示",
    {
      type: "warning",
    }
  ).then(async () => {
    const res = await payChannelHideApi({ id: row.id, is_hide: type });
    getTbaleData();
    ElMessage.success(res.message);
  });
};

const handleUploader = (res) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });
    dialogForm.icon = res.data.name || "";
    dialogForm.iconUrl = res.data.pic_url || "";
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await payChannelListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 查询
const onAdd = () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  delete param.iconUrl
  delete param.icon_show
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id
      const res = await payChannelAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      const res = await payChannelUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
